package com.lucky.ebidding.controller;

import com.lucky.ebidding.dto.NotificationDTO;
import com.lucky.ebidding.dto.ZbDTO;
import com.lucky.ebidding.dto.ZbInfoAndModify;
import com.lucky.ebidding.dto.ZbModifyDTO;
import com.lucky.ebidding.model.User;
import com.lucky.ebidding.model.ZbInfo;
import com.lucky.ebidding.service.NotificationService;
import com.lucky.ebidding.service.ZbService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * ClassName:indexController
 * Package:com.lucky.ebidding.controller
 * Description:
 *
 * @Date:2020/3/13 15:42
 * @Author:857251389@qq.com
 */
@Controller
public class IndexController {
    @Autowired
    private ZbService zbService;
    @Autowired
    private NotificationService notificationService;

    @GetMapping({"/","/index"})
    public String index(Model model, HttpServletRequest request) {
        User user = (User) request.getSession().getAttribute("user");
        if(user!=null){
            List<NotificationDTO> notificationDTOS = notificationService.selectNotificationByReceiver(user.getId(),0);
            model.addAttribute("unReadSize",notificationDTOS.size());
        }
        return "index";
    }

    @ResponseBody
    @GetMapping("/weiKaiBiao")
    public ZbDTO weiKaiBiao(Integer kbFlag){
        List<ZbInfo> zbInfos = zbService.selectByShFlagAndKbFlag(1,kbFlag);
        ZbDTO zbDTO = new ZbDTO();

        zbDTO.setCount(zbInfos.size());
        zbDTO.setMsg("yes");
        zbDTO.setData(zbInfos);
        zbDTO.setCode(0);

        return zbDTO;
    }

    @ResponseBody
    @GetMapping("/getZbModifyData")
    public ZbModifyDTO getZbModifyData(){

        List<ZbInfoAndModify> zbInfoAndModifyList =  zbService.selectZbInfoAndZbModifyByShFlag(1);

        ZbModifyDTO zbModifyDTO = new ZbModifyDTO();
        zbModifyDTO.setCode(0);
        zbModifyDTO.setCount(zbInfoAndModifyList.size());
        zbModifyDTO.setData(zbInfoAndModifyList);
        zbModifyDTO.setMsg("yes");

        return zbModifyDTO;
    }
}
